import java.io.IOException;
import java.io.StringReader;

/**
 * Class for a Chessboard for Knight Game.
 * ***Jordan: I have changed the board type to be a String... 
 * 
 * @author Jordan Beland, modified by Sabrina Gaudreau
 *
 */
public class Board {

    public static int SIZE = 8;
    private String[][] board;
        
    /**
     * Constructor for Board object.
     * Initializes each of the board positions
     * to false to imply there is no
     * piece at that location.
     * @author Jordan Beland
     */
    public Board() {
        board = new String[SIZE][SIZE];
        for(int i = 0; i < SIZE; i++){
            for(int j = 0; j < SIZE; j++){
                board[i][j] = "|_|";
            }
        }
    }
    
    /**
     * Notifies board that a piece has been placed
     * at given coordinates.
     * 
     * @author Jordan Beland
     * @param x
     * @param y
     */
    public void placePiece(int x,int y, String rep){
        board[x][y] = "|" + rep + "|";
    }
    
    
    /**
     * Returns if the coordinates are true or false
     * 
     * @author Sabrina Gaudreau
     * @param x
     * @param y
     */
    public String getSquare(int x, int y){
        return board[x][y];
    }
    
    /**
     * Notifies board that a piece has been removed
     * at given coordinates.
     * 
     * @author Jordan Beland
     * @param x
     * @param y
     */
    // *** Jordan. I have changed my board type to be a string
    // so that I can tell the board where a specific piece (ex: Queen, Knight, etc)
    // is located...
    //public void removePiece(int x,int y){
    //  board[x][y] = false;
    //}
    
    /**
     * Default getter for board instance variable.
     * 
     * @author Jordan Beland
     * @return Board object
     */
    public String[][] getBoard() {
        return board;
    }
    
    /**
     * to String method, creates an empty cell if there is
     * no piece at that coordinate, or places a 'Q' in the 
     * cell if there is a queen object there.
     * @author Jordan Beland, modified by Sabrina Gaudreau
     */
    public String toString(){
        String cell = "";
        cell += "  A  B  C  D  E  F  G  H \n";        
        //cell += "  _  _  _  _  _  _  _  _ \n";
        for(int j = 0; j < SIZE; j++){
        	cell += j + 1;
            for(int i = 0; i < SIZE; i++){
                cell += board[i][j];
            }
            cell += "\n";
        }
        return cell;
    }  
    
    public void print(){
        System.out.println(this);
    }
    
        /**
     * 
     * @param position: The position on the chess board (i.e. A6).
     * @return The x coordinate of the position as an integer.
     * @throws IOException
     * @author - Vivian Andreeva, modified by Sabrina Gaudreau
     */
    public int convertx(String position) throws IOException {
        // Ex: Position input by user is A6. convertx converts the letter "A" into 
        // a numerical value (0-7) to use in the chess grid.
        StringReader in = new StringReader(position);
        // Reads first character, in this case, "A".
        int c = in.read();
        // Converts character into a usable String, s. 
        String s = Character.toString((char)c);
        // Compare string to various APPLICABLE letters. 
        // If the input is valid, it returns the integer corresponding to the specific letter
        // as the x coordinate (A = 0).
        if (s.compareTo("A") == 0){
            return 0;
        }
        else if (s.compareTo("B") == 0) {
            return 1;
        }
        else if (s.compareTo("C") == 0) {
            return 2;
        }
        else if (s.compareTo("D") == 0) {
            return 3;
        }
        else if (s.compareTo("E") == 0) {
            return 4;
        }
        else if (s.compareTo("F") == 0) {
            return 5;
        }
        else if (s.compareTo("G") == 0) {
            return 6;
        }
        else if (s.compareTo("H") == 0) {
            return 7;
        }
        else if (s.compareTo("Q") == 0) { // if user wants to quit the game
            return 8;
        }
        else if (s.compareTo("M") == 0) { // view available moves
            return 9;
        }
        //If the letter is not applicable to any of the letters (A-H), the input 
        // is invalid (returns -1) and must be retyped.
        return -1;
    }
    /**
     * 
     * @param position: The position on the chess board (i.e. A6).
     * @return The y coordinate of the position as an integer. 
     * @throws IOException
     * @author - Vivian Andreeva
     */
    public int converty(String position) throws IOException {
        // Ex: Position input by user is A6. converty takes the second character, (6 in this
        // example) and returns it as an integer y coordinate.
        StringReader in = new StringReader(position);
        int i = 0;
        int c = 0;
        do {
            c = in.read();
            i++;
        // Loop through this function twice so that c is equal to the second character, 6, and
        // not the first, A. 
        } while (i<2);
        int a = Integer.parseInt(Character.toString((char) c));
        return (a-1);
    }
}
        
        



